/**
 * js处理php转码输出
 * @param  {[type]} str [description]
 * @return {[type]}     [description]
 */
function htmlDecode(s) {
    if (!s) return;
    s = s.replace(/&amp;/g, "&");
    s = s.replace(/&lt;/g, "<");
    s = s.replace(/&gt;/g, ">");
    s = s.replace(/&apos;/g, "'");
    s = s.replace(/&quot;/g, "\"");
    return s;
}
/**
 * 取值
 */
function getQueryString(url, name1) {
    if (typeof name1 !== 'undefined') {
        url = url.substring(url.indexOf('?'));
        var reg = new RegExp("(^|&)" + name1 + "=([^&]*)(&|$)");
        var r = url.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }
}
/**
 * 替换参数
 * @param  {[type]} url  [description]
 * @param  {[type]} name [description]
 * @param  {[type]} val  [description]
 * @return {[type]}      [description]
 */
function setQueryString(url, name, val) {
    var s;
    var reg = new RegExp(name + "=([^&]*)(&|$)", "ig");
    var h = reg.exec(url);
    if (!h) {
        if (url.substr(-1, 1) == '&') {
            url = url.substr(0, url.length - 1);
        }
        s = (url.indexOf("?") == "-1") ? '?' + name + "=" + val : "&" + name + "=" + val;
        s = url + s;
    } else {
        if (!val) {
            s = url.replace(h[0], h[2])
        } else {
            s = url.replace(h[0], name + '=' + val + h[2])
        }
    }
    if (s.substr(-1, 1) == '&') {
        s = s.slice(0, -1);
    }
    return s;
}
/**
 * resize image
 * @param {[type]} maxWidth  [description]
 * @param {[type]} maxHeight [description]
 * @param {[type]} objImg    [description]
 */
function resizeImage(objImg, maxWidth, maxHeight) {
    var img = new Image();
    img.src = objImg.src;
    var hRatio;
    var wRatio;
    var Ratio = 1;
    var w = img.width;
    var h = img.height;
    wRatio = maxWidth / w;
    hRatio = maxHeight / h;
    if (maxWidth == 0 && maxHeight == 0) {
        Ratio = 1;
    } else if (maxWidth == 0) { //
        if (hRatio < 1) Ratio = hRatio;
    } else if (maxHeight == 0) {
        if (wRatio < 1) Ratio = wRatio;
    } else if (wRatio < 1 || hRatio < 1) {
        Ratio = (wRatio <= hRatio ? wRatio : hRatio);
    }
    if (Ratio < 1) {
        w = w * Ratio;
        h = h * Ratio;
    }
    $(objImg).width(w);
    $(objImg).height(h);
}
//提取cookie
document.getCookie = function(sName) {
    // cookies are separated by semicolons
    var aCookie = document.cookie.split("; ");
    for (var i = 0; i < aCookie.length; i++) {
        // a name/value pair (a crumb) is separated by an equal sign
        var aCrumb = aCookie[i].split("=");
        if (sName == aCrumb[0]) return decodeURIComponent(aCrumb[1]);
    }
    // a cookie with the requested name does not exist
    return null;
}
//赋值cookie
document.setCookie = function(sName, sValue, sExpires) {
    var sCookie = sName + "=" + encodeURIComponent(sValue);
    if (sExpires != null) {
        sCookie += "; expires=" + sExpires;
    }
    document.cookie = sCookie;
}
//删除cookie
document.removeCookie = function(sName, sValue) {
    document.cookie = sName + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}

function preloadimages(arr) {
    var newimages = []
    var arr = (typeof arr != "object") ? [arr] : arr //确保参数总是数组
    for (var i = 0; i < arr.length; i++) {
        newimages[i] = new Image()
        newimages[i].src = arr[i]
    }
}
//图片错误处理
window.addEventListener('error', function(e) {
    // 当前异常是由图片加载异常引起的
    if (e && e.target && e.target.tagName  && e.target.tagName.toUpperCase() === 'IMG') {
        e.target.src = '/static/images/noimg.jpg';
    }
}, true)